using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using EcsSiteDSTableAdapters;

namespace SiteECSWeb
{
    [DataObject]
   public class FairsBLL
   {
       private FairsTableAdapter _FairsAdapter = null;

       protected FairsTableAdapter FairsAdapter
       {
           get
           {
               if (_FairsAdapter == null)
               {
                   _FairsAdapter = new FairsTableAdapter();
               }
               return _FairsAdapter;
           }

       }


       #region SelectFairs

       public int GetTotalNumberOfFairs()
       {
           Nullable<int> totalNull =
               (Nullable<int>)FairsAdapter.TotalNumberOfFairs();
           return totalNull.GetValueOrDefault();
       }
       public int GetTotalNumberOfFairs(string FilterStr)
       {
           if (string.IsNullOrEmpty(FilterStr))
           {
               return GetTotalNumberOfFairs();
           }
           else
           {
               Nullable<int> totalNull = (Nullable<int>)FairsAdapter.
               TotalNumberOfFairsByFilterExp(FilterStr);
               return totalNull.GetValueOrDefault();
           }
       }
        //Get All Faris Data //
       [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
       public EcsSiteDS.FairsDataTable GetAllFairsData()
       {
           return FairsAdapter.GetFairsRowRankData();
       }

        //Get Fairs Data By Fair Id //
       [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
       public EcsSiteDS.FairsDataTable GetFairsByFairID(int Fars_id)
       {
           if (Fars_id <=0)
           {
               return FairsAdapter.GetFairsRowRankData();
           }
           else
           {
               return FairsAdapter.GetFairsDataByFairID(Fars_id);
           }
       }


       [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
       public EcsSiteDS.FairsDataTable GetFairsByfairNmArFrstLetter(string Fair_nm ,int TopCount)
       {
           return FairsAdapter.
               GetFairsDataByfairNmArFrstLetter(Fair_nm , TopCount);
       }

       [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
       public EcsSiteDS.FairsDataTable GetFarisByRowRank(int RowRank)
       {
           return FairsAdapter.GetFairsDataByRowRank(RowRank);
       }

       [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
       public EcsSiteDS.FairsDataTable GetFairsByCountryNmEn(string CountryNmen)
       {
           return FairsAdapter.GetFairsDataByCuntryEn(CountryNmen);
       }
       //
       [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
       public EcsSiteDS.FairsDataTable
           GetFairsByCityIDYearMonth(int cityId, int year, int month)
       {
           return FairsAdapter.GetFairsDataByCityIdYearMonth(cityId, year, month);
       }


       // To Get all Top 5 Fairs Data for the first Page//
       [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
       public EcsSiteDS.FairsDataTable GetFairsTopfiveRowNum()
       {
           return FairsAdapter.GetFairsDataTopRowNum();
       }

       
       [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
       public EcsSiteDS.FairsDataTable GetFairsAsGridPagesAndFilterExpression(int startRowIndex,
           int maximumRows, string sortExpression)
       {
           return FairsAdapter.
               GetFairsDataByGridPages(startRowIndex, maximumRows, sortExpression);
       }

       [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
       public EcsSiteDS.FairsDataTable 
           GetFairsAsGridPagesAndFilterExpression(int startRowIndex, int maximumRows,
           string FilterStr, string sortExpression)
       {
           if (string.IsNullOrEmpty(FilterStr))
           {
               return
                   GetFairsAsGridPagesAndFilterExpression(startRowIndex, maximumRows, sortExpression);
           }
           else
           {
               return FairsAdapter.GetFairsDataByGridPagesFilterExp
                   (startRowIndex, maximumRows, FilterStr, sortExpression);
           }
       }

       [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
       public PagedDataSource GetFairsByCuntryEnAsPagedDataSource
           (string CuntryEn, int pageIndex, int pageSize)
       {
           EcsSiteDS.FairsDataTable fairsTable = GetFairsByCountryNmEn(CuntryEn);

           PagedDataSource pagedData = new PagedDataSource();
           pagedData.DataSource = fairsTable.Rows;
           pagedData.AllowPaging = true;
           pagedData.CurrentPageIndex = pageIndex;
           pagedData.PageSize = pageSize;
           return pagedData;
       }


       #endregion

       #region InsertFairs
       [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
       public int AddFairs(string Fair_nm, string Fair_LogoIcon, string Fair_desc, string Fair_address, DateTime Fair_strtdate, DateTime Fair_enddate,
           string Fair_email, string Fair_website, int Wcomp_id, string Notes, bool? Formemberonly, bool? Publishing, string Fair_Type, string Fair_Path, string Fair_ImagePath)
      
       {
           EcsSiteDS.FairsDataTable FairsTable = new EcsSiteDS.FairsDataTable();
           EcsSiteDS.FairsRow Fairs_Row = FairsTable.NewFairsRow();
           if (string.IsNullOrEmpty(Fair_LogoIcon) ) Fairs_Row.SetFair_LogoIconNull(); else Fairs_Row.Fair_LogoIcon = Fair_LogoIcon;
           if (string.IsNullOrEmpty(Fair_desc)) Fairs_Row.SetFair_descNull(); else Fairs_Row.Fair_desc = Fair_desc;
           if (string.IsNullOrEmpty(Fair_address) ) Fairs_Row.SetFair_addressNull(); else Fairs_Row.Fair_address = Fair_address;
           if (string.IsNullOrEmpty(Fair_email) ) Fairs_Row.SetFair_emailNull(); else Fairs_Row.Fair_email = Fair_email;
           if (string.IsNullOrEmpty(Fair_website) ) Fairs_Row.SetFair_websiteNull(); else Fairs_Row.Fair_website = Fair_website;
           if (string.IsNullOrEmpty(Notes) ) Fairs_Row.SetNotesNull(); else Fairs_Row.Notes = Notes;
           if (Formemberonly == null) Fairs_Row.SetFormemberonlyNull(); else Fairs_Row.Formemberonly = Formemberonly.Value;
           if (Publishing == null) Fairs_Row.SetPublishingNull(); else Fairs_Row.Publishing = Publishing.Value;
           if (string.IsNullOrEmpty(Fair_Type )) Fairs_Row.SetFair_TypeNull(); else Fairs_Row.Fair_Type = Fair_Type;
           if (string.IsNullOrEmpty(Fair_Path )) Fairs_Row.SetFair_pathNull(); else Fairs_Row.Fair_path = Fair_Path;
           if (string.IsNullOrEmpty(Fair_ImagePath)) Fairs_Row.SetFair_ImagePathNull(); else Fairs_Row.Fair_ImagePath = Fair_ImagePath;

           Fairs_Row.Fair_nm = Fair_nm;
           Fairs_Row.Fair_strtdate =Fair_strtdate;
           Fairs_Row.Fair_enddate=Fair_enddate;
           Fairs_Row.Wcomp_id = Wcomp_id;
           
           FairsTable.AddFairsRow(Fairs_Row);

           int rowsAffected = FairsAdapter.Update(FairsTable);

           return (rowsAffected == 1) ? Fairs_Row.Fair_id : -1;
       }
           
       #endregion

       #region UpdateFairs
       [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
       public bool UpdateFairs(string Fair_nm, string Fair_LogoIcon, string Fair_desc, string Fair_address,
         DateTime Fair_strtdate, DateTime Fair_enddate, string Fair_email,
         string Fair_website, int Wcomp_id, string Notes,
         bool? Formemberonly, bool? Publishing, string Fair_Type, string Fair_Path, string Fair_ImagePath, int Fair_id)
       {
           EcsSiteDS.FairsDataTable FairsTable = FairsAdapter.GetFairsDataByFairID(Fair_id);
           if (FairsTable.Count == 0)
           {
               return false;
           }

           EcsSiteDS.FairsRow Fairs_Row = FairsTable[0];
           if (string.IsNullOrEmpty(Fair_LogoIcon)) Fairs_Row.SetFair_LogoIconNull(); else Fairs_Row.Fair_LogoIcon = Fair_LogoIcon;
           if (string.IsNullOrEmpty(Fair_desc )) Fairs_Row.SetFair_descNull(); else Fairs_Row.Fair_desc = Fair_desc;
           if (string.IsNullOrEmpty(Fair_address )) Fairs_Row.SetFair_addressNull(); else Fairs_Row.Fair_address = Fair_address;
           if (string.IsNullOrEmpty(Fair_Path)) Fairs_Row.SetFair_pathNull(); else Fairs_Row.Fair_path = Fair_Path;
           if (string.IsNullOrEmpty(Fair_ImagePath)) Fairs_Row.SetFair_ImagePathNull(); else Fairs_Row.Fair_ImagePath = Fair_ImagePath;

           if (string.IsNullOrEmpty(Fair_email)) Fairs_Row.SetFair_emailNull(); else Fairs_Row.Fair_email = Fair_email;
           if (string.IsNullOrEmpty(Fair_website )) Fairs_Row.SetFair_websiteNull(); else Fairs_Row.Fair_website = Fair_website;
           if (string.IsNullOrEmpty(Notes )) Fairs_Row.SetNotesNull(); else Fairs_Row.Notes = Notes;
           if (Formemberonly == null) Fairs_Row.SetFormemberonlyNull(); else Fairs_Row.Formemberonly = Formemberonly.Value;
           if (Publishing == null) Fairs_Row.SetPublishingNull(); else Fairs_Row.Publishing = Publishing.Value;
           if (string.IsNullOrEmpty(Fair_Type )) Fairs_Row.SetFair_TypeNull(); else Fairs_Row.Fair_Type = Fair_Type;
           
           Fairs_Row.Fair_nm = Fair_nm;
           Fairs_Row.Fair_strtdate = Fair_strtdate;
           Fairs_Row.Fair_enddate = Fair_enddate;
           Fairs_Row.Wcomp_id = Wcomp_id;
           
           int rowsAffected = FairsAdapter.Update(Fairs_Row);

           return rowsAffected == 1;

       }

       #endregion

       #region DeleteFairs

       [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
       public bool DeleteFairs(int Fair_id)
       {
           EcsSiteDS.FairsDataTable FairsTable = FairsAdapter.GetFairsDataByFairID(Fair_id);
           if (FairsTable.Count == 0)
           {
               return false;
           }

           EcsSiteDS.FairsRow Fairs_Row = FairsTable[0];

           if (!Fairs_Row.IsFair_ImagePathNull())
           {
               System.IO.File.Delete(HttpContext.Current.Server.MapPath(Globals.Settings.UploadInfo.RootAttachFolder
                         + Fairs_Row.Fair_ImagePath));
           }
           if (!Fairs_Row.IsFair_pathNull())
           {
               System.IO.File.Delete(HttpContext.Current.Server.MapPath(Globals.Settings.UploadInfo.RootAttachFolder
                       + Fairs_Row.Fair_path));
           }
           int rowsAffected = FairsAdapter.Delete(Fair_id);
           return rowsAffected == 1;
       }
       #endregion

   }

}
